package com.nit.lab.controller.system;

import com.alibaba.fastjson.JSONObject;
import com.nit.lab.dto.system.SchoolDTO;
import com.nit.lab.dto.utils.OpResultDTO;
import com.nit.lab.dto.utils.TableReqDTO;
import com.nit.lab.dto.utils.TableRspDTO;
import com.nit.lab.service.system.SchoolService;
import com.nit.lab.utils.HttpRequestReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping(value = "/api/school")
public class SchoolController {

    @Resource
    private SchoolService schoolService;
    private final Logger logger = LoggerFactory.getLogger(SchoolController.class);

    /**
     * @Description: 获取数据列表
     * @Author: SN
     * @Date: 2020/02/30 18:15
     */
    @RequestMapping(value = "/query", method = RequestMethod.POST)
    public TableRspDTO list4Table(@RequestBody TableReqDTO tableReqDTO) {
        TableRspDTO tableRspDTO = new TableRspDTO();
        try {
            tableRspDTO = schoolService.list4Table(tableReqDTO);
        } catch (Exception e) {
            logger.error(e.toString());
        }
        return tableRspDTO;
    }

    /**
     * @Description: 新增记录
     * @Author: SN
     * @Date: 2020/02/30 18:15
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public OpResultDTO add(@RequestBody SchoolDTO schoolDTO) {
        OpResultDTO opResult = new OpResultDTO();
        try {
            opResult.setMessage(schoolService.add(schoolDTO).toString());
        } catch (Exception e) {
            logger.error(e.toString());
            opResult.setMessage("error");
        }
        return opResult;
    }

    /**
     * @Description: 更新记录
     * @Author: SN
     * @Date: 2020/02/30 18:15
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public OpResultDTO update(@RequestBody SchoolDTO schoolDTO) {
        OpResultDTO opResult = new OpResultDTO();
        try {
            opResult.setMessage(schoolService.update(schoolDTO).toString());
        } catch (Exception e) {
            logger.error(e.toString());
            opResult.setMessage("error");
        }
        return opResult;
    }

    /**
     * @Description: 删除记录
     * @Author: SN
     * @Date: 2020/02/30 18:15
     */
    @RequestMapping(value = "/remove", method = RequestMethod.POST)
    public OpResultDTO remove(HttpServletRequest request) {
        OpResultDTO opResult = new OpResultDTO();
        try {
            JSONObject jsonRequest = HttpRequestReader.getJsonObject(request);
            String schoolIds = jsonRequest.getString("schoolIds");
            opResult.setMessage(schoolService.remove(schoolIds).toString());
        } catch (Exception e) {
            logger.error(e.toString());
            opResult.setMessage("error");
        }
        return opResult;
    }

    /**
     * @Description: 查询学校list
     * @Author: zxq
     * @Date: 2020/8/20
     */
    @RequestMapping(value = "/listSchool", method = RequestMethod.POST)
    public OpResultDTO listSchool() {
        OpResultDTO opResult = new OpResultDTO();
        try {
            opResult.setMessage("success");
            opResult.setResult(schoolService.listSchool());
        } catch (Exception e) {
            logger.error(e.toString());
            opResult.setMessage("error");
        }
        return opResult;
    }
}
